﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace BRS.BidModule
{
    public partial class Userfrm : Form
    {
        DateTime datetime;
        Entities brs = new Entities();
        int userid;
        public Userfrm()
        {
            InitializeComponent();
        }
        public Userfrm(Users users)
        {
            InitializeComponent();
            //Users user = new Users()
            {
                textBox1.Text = users.UserName;
                //textBox2.Text = users.Password;
                textBox3.Text = users.UserCode;
                textBox6.Text = users.UkeyCode;
                textBox7.Text = users.UkeyPassword;
                checkBox1.Checked = Enabled;
                datetime = users.AddTime;


            };
            userid = users.UserId;
        }
        private void button1_Click(object sender, EventArgs e)
        {

            if (textBox1.Text != null && textBox1.Text != "")
            {
                //if (brs.Users.Where(p => p.UserName == textBox1.Text).Count() > 0)
                //{ MessageBox.Show("该用户名已经存在，请选择其他用户名！"); }
                //else
                {
                    if (datetime == null) { datetime = DateTime.Now; }
                    byte[] result = Encoding.Default.GetBytes(this.textBox2.Text.Trim());    //tbPass为输入密码的文本框
                    MD5 md5 = new MD5CryptoServiceProvider();
                    byte[] output = md5.ComputeHash(result);
                    string MD5password = BitConverter.ToString(output).Replace("-", "");  //tbMd5pass为输出加密文本的文本框
                    //Users newuser = new Users()
                    //{
                    //    UserName = textBox1.Text,
                    //    Password = MD5password,
                    //    UserCode = textBox3.Text,
                    //    UkeyCode = textBox6.Text,
                    //    UkeyPassword = textBox7.Text,
                    //    Enabled = checkBox1.Checked,
                    //    AddTime = DateTime.Now

                    //};
                    if (userid > 0)
                    {
                        var user = brs.Users.Find(userid);
                        if (user != null)
                        {
                            user.UserName = textBox1.Text;
                            user.Password = MD5password;
                            user.UserCode = textBox3.Text;
                            user.UkeyCode = textBox6.Text;
                            user.UkeyPassword = textBox7.Text;
                            user.Enabled = checkBox1.Checked;

                        }
                    }
                    int ok = brs.SaveChanges();
                    if (ok == 1) MessageBox.Show("修改成功！");
                }

            }
        }
    }
}
